package com.cw.controller;

import com.cw.common.api.ResultObject;
import com.cw.common.constant.Constants;
import com.cw.common.constant.Response;
import com.cw.dto.RoleListDto;
import com.cw.entity.SysRole;
import com.cw.entity.SysUser;
import com.cw.service.ISysRoleMenuService;
import com.cw.service.ISysRoleService;
import com.cw.service.ISysUserRoleService;
import com.cw.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
* @author smq
* @since 2021-04-28
*/
@Api(tags = {"用户角色管理"})
@RestController
@RequestMapping("/sys-role")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysRoleMenuService roleMenuService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    /**
     * 查询所有角色
     *
     * @return
     */
    @ApiOperation(value = "分页查询角色")
    @PostMapping("/getAllRoles")
    public ResultObject getAllRoles(@RequestBody RoleListDto roleListDto) {
        //当前页
        Integer pageNum = roleListDto.getPageNum();
        //显示条数
        Integer pageSize = roleListDto.getPageSize();
        String roleName = "";
        if(StringUtil.isNotEmpty(roleListDto.getRoleName())){
            roleName = roleListDto.getRoleName();
        }
        PageHelper.startPage(pageNum, pageSize);
        List<SysRole> sysRole = sysRoleService.getSysRole(null, roleName, null);
        PageInfo<SysRole> sysRolePage = new PageInfo<>(sysRole);
        return ResultObject.successReturn(sysRolePage);
    }

    /**
     * 添加角色
     *
     * @return
     */
    @ApiOperation(value = "添加角色")
    @PostMapping("/insertRole")
    public Response insertRole(@RequestBody SysRole sysRole) {
        //判断是否已存在
        List<SysRole> sysRoleExist1 = sysRoleService.getSysRoleByRoleName(sysRole.getRoleName());
        //已存在此角色名
        if (null != sysRoleExist1 && sysRoleExist1.size() > 0) {
            return new Response(500, "角色名称已存在");
        }
        sysRole.setRoleCode(UUIDUtil.uuid());
        List<SysRole> sysRoleExist2 = sysRoleService.getSysRole(null, null, sysRole.getRoleCode());
        //已存在此角色编码
        if (null != sysRoleExist2 && sysRoleExist2.size() > 0) {
            return new Response(500, "角色编码已存在");
        }
        sysRole.setCreateTime(LocalDateTime.now());
        int insertSysRole = sysRoleService.insertSysRole(sysRole);
        if (insertSysRole > 0) {
            return new Response();
        } else {
            return new Response(500, "角色新增失败");
        }
    }

    /**
     * 修改角色
     *
     * @return
     */
    @ApiOperation(value = "修改角色")
    @PostMapping("/updateRole")
    public Response updateRole(@RequestBody SysRole sysRole) {
        //判断是否已存在
        List<SysRole> sysRoleExist1 = sysRoleService.getSysRoleByRoleName(sysRole.getRoleName());
        boolean updateSysRole;
        //已存在此角色名
        if (!CollectionUtils.isEmpty(sysRoleExist1)) {
            for (SysRole role : sysRoleExist1) {
                if (role.getRoleId().equals(sysRole.getRoleId())) {
                    sysRoleService.updateById(sysRole);
                    return new Response();
                }
            }
            return new Response(500, "角色名称已存在");
        }

        updateSysRole = sysRoleService.updateById(sysRole);
        if (updateSysRole) {
            return new Response();
        } else {
            return new Response(500, "编辑角色失败");
        }
    }


    @ApiOperation(value = "查询角色")
    @GetMapping("/getRole")
    public Response getRole(@RequestParam String roleId) {
        List<SysRole> sysRole = sysRoleService.getSysRole(roleId, null, null);
        if (sysRole != null && sysRole.size() > 0) {
            return new Response(200, "角色查询成功", sysRole.get(0));
        } else {
            return new Response(500, "角色查询失败");
        }
    }

    @ApiOperation("查询角色以及他的权限")
    @GetMapping("/getRoleAndAuthority")
    public ResultObject getRoleAndAuthority(@RequestParam String roleId) {
        return ResultObject.successReturn(roleMenuService.getRoleAndAuthorityTree(roleId));
    }

    @ApiOperation(value = "不分页查询角色列表")
//    @ApiImplicitParam(name = "roleName", value = "角色名称", required = false, paramType = "query", dataType = "String")
    @PostMapping("/getRolesList")
    public Response getRolesList(String roleName) {
        if (null == roleName || "".equals(roleName)) {
            roleName = null;
        }
        List<SysRole> sysRole = sysRoleService.getSysRole(null, roleName, null);
        if (sysRole != null && sysRole.size() > 0) {
            return new Response(200, "角色查询成功", sysRole);
        } else {
            return new Response(500, "角色查询失败");
        }
    }

    /**
     * 删除角色
     *
     * @return
     */
    @ApiOperation(value = "删除角色")
    //    @ApiImplicitParam(name = "roleId", value = "角色Id", required = true, paramType = "query", dataType = "Integer")
    @PostMapping("/deleteRole/{roleId}")
    public Response deleteRole(@PathVariable String roleId) {
        List<SysUser> userByRoleId = sysUserRoleService.getUserByRoleId(roleId);
        if (!CollectionUtils.isEmpty(userByRoleId)) {return new Response(500, "删除角色失败,因当前角色内还有人员");}
        int deleteSysRole = sysRoleService.deleteSysRole(roleId);
        if (deleteSysRole > 0) {
            return new Response();
        } else {
            return new Response(500, "删除角色失败");
        }
    }

    /**
     * 批量删除角色
     *
     * @return
     */
    @RequestMapping("/deleteManyRole")
    public int deleteManyRole(@RequestBody Map<String, Object> map) {
        List<String> roleIds = (List<String>) map.get("roleIds");
        int deleteManySysRole = sysRoleService.deleteManySysRole(roleIds);
        if (deleteManySysRole > 0) {
            return Constants.success;
        } else {
            return Constants.fail;
        }
    }


    /**
     * 角色是否存在
     * @return
     */
    @RequestMapping("/isExistRole")
    public int isExistRole(@RequestParam String roleName) {
        List<SysRole> sysRole = sysRoleService.getSysRole(null, roleName, null);
        //已存在此角色
        if (null != sysRole && sysRole.size() > 0) {
            return Constants.fail;
        } else {
            return Constants.success;
        }
    }
}

